package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Voice;
import com.itheima.service.mongo.VideoService;
import com.itheima.service.mongo.VoiceService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;

@DubboService
public class VoiceServiceImpl implements VoiceService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RedisIdService redisIdService;

    //桃花传音-发送语音
    @Override
    public void saveVoice(Voice voice) {
        //voice添加vid
        voice.setVid(redisIdService.getNextId(ConstantUtil.VIDEO_ID));

        //存入mongoDB
        mongoTemplate.save(voice);
    }

    //随机获取语音
    @Override
    public Voice findUserId() {
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(new Criteria()), Aggregation.sample(1));
        AggregationResults<Voice> voices = mongoTemplate.aggregate(aggregation,
                Voice.class, Voice.class);
        Voice voice = new Voice();
        for (Voice v : voices) {
            voice = v;
        }
        return voice;
    }
}